//
//  MineViewController.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/2/15.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "MineViewController.h"
#import "CustomTableViewCellOne.h"
#import "CustomTableViewCellTwo.h"
#import "CustomMineTableViewCellThree.h"
#import "MyOrderFormViewController.h"
#import "AddressManagerViewController.h"
#import "AccountManager.h"
#import "MineHeadView.h"
#import "AppContext.h"
#import "LoginViewController.h"
#import "InvitationViewController.h"
#import "WXApi.h"
@interface MineViewController (){
    MineHeadView *headView;
    NSArray *titles;
    NSArray *images;
    float scale;
}
@property(nonatomic, strong) InvitationViewController *inviteVC;
@property(nonatomic, strong) UIView *backView;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    titles=[NSArray arrayWithObjects:@"收货地址管理",@"关注公众微信号",@"给春来庄打分吧",@"邀请有礼",@"联系客服", nil];
    
    images=[NSArray arrayWithObjects:@"icon_dizhi",@"icon_weixin",@"icon_dafen",@"icon_yaoqing",@"icon_kefu", nil];
    self.navigationController.navigationBarHidden=YES;
    self.displayView.contentInset = UIEdgeInsetsMake(180 , 0, 0, 0);
    self.displayView.showsVerticalScrollIndicator=NO;
    self.displayView.bounces=NO;
    self.displayView.backgroundColor=[UIColor clearColor];
    [self.displayView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    headView= [[[NSBundle mainBundle] loadNibNamed:@"MineHeadView"  owner:self options:nil] lastObject];
    headView.frame=CGRectMake(0, -180, SCREEN_WIDTH, 180);
    scale=headView.frame.size.width/headView.frame.size.height;
    [self.displayView addSubview:headView];
    [self.view addSubview:self.displayView];
    [self setupNotification];
    // Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [APP_CONTEXT.accountManager refreshAccountInfo];
    [headView refreshHeadView];
    [self.displayView reloadData];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UITableView*)displayView{
    if (!_displayView) {
        _displayView=[[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-49)];
        _displayView.delegate=self;
        _displayView.dataSource=self;
        _displayView.backgroundColor=[UIColor clearColor];
        [_displayView setSeparatorColor:[UIColor colorWithHexString:@"e1e1e1"]];
    }
    return _displayView;
}

- (UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _backView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6f];
        [self.navigationController.view addSubview:_backView];
    }
    return _backView;
}

- (InvitationViewController *)inviteVC {
    if (!_inviteVC) {
        _inviteVC = [[InvitationViewController alloc] init];
        _inviteVC.view.backgroundColor = [UIColor clearColor];
        __weak typeof(self) weakSelf = self;
        _inviteVC.closePageBlock = ^(){
            weakSelf.backView.alpha = 0.f;
        };
    }
    return _inviteVC;
}

#pragma mark-UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 8;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
        {
            static NSString *identifier = @"CustomTableViewCellTwo";
            CustomTableViewCellTwo* cell = [tableView dequeueReusableCellWithIdentifier:
                                            identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCellTwo"  owner:self options:nil] lastObject];
            }
            [cell updateNumInfo];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            return cell;
        }
            break;
        case 1:
        {
            static NSString *identifier = @"CustomTableViewCellOne";
            CustomTableViewCellOne* cell = [tableView dequeueReusableCellWithIdentifier:
                                            identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCellOne"  owner:self options:nil] lastObject];
                
            }
            cell.iconImageView.image=[UIImage imageNamed:@"icon_dingdan"];
            cell.titleLabel.text=@"我的订单";
            cell.detailLable.hidden=NO;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case 2:
        {
            static NSString *identifier = @"CustomMineTableViewCellThree";
            CustomMineTableViewCellThree* cell = [tableView dequeueReusableCellWithIdentifier:
                                                  identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomMineTableViewCellThree"  owner:self options:nil] lastObject];
                
            }
            [cell loadData];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            return cell;
        }
            break;
            
        default:
        {
            static NSString *identifier = @"CustomTableViewCellOne";
            CustomTableViewCellOne* cell = [tableView dequeueReusableCellWithIdentifier:
                                            identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCellOne"  owner:self options:nil] lastObject];
                
            }
            cell.iconImageView.image=[UIImage imageNamed:images[indexPath.row-3]];
            cell.titleLabel.text=titles[indexPath.row-3];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            if (indexPath.row==6) {
                [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            }
            return cell;
            
        }
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            return 70;
            break;
        case 1:
            return 44;
            break;
        case 2:
            return 72;
            break;
        default:
            return 44;
            
            break;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 1:
        {
            if (!APP_CONTEXT.account) {
                [LoginViewController presentLoginViewController:self withCompleteBlock:nil];
                return;
            }
            MyOrderFormViewController *orderVC=[[MyOrderFormViewController alloc] init];
            orderVC.selectIndex=0;
            [self.navigationController pushViewController:orderVC animated:YES];
        }
            break;
        case 3:
        {
            if (!APP_CONTEXT.account) {
                [LoginViewController presentLoginViewController:self withCompleteBlock:nil];
                return;
            }
            AddressManagerViewController *addressVC = [[AddressManagerViewController alloc] init];
            [self.navigationController pushViewController:addressVC animated:YES];
            
        }
            break;
        case 4:
        {
            //关注公众微信号
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = @"HC春来庄";
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"已成功复制微信公众账号，请到微信中关注我们" message:@"" delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
            [alertView show];
            
        }
            break;
        case 5:
        {
            //给春来庄打分
            
        }
            break;
            
        case 6:
        {
            //邀请有礼
            if (!APP_CONTEXT.account) {
                [LoginViewController presentLoginViewController:self withCompleteBlock:nil];
                return;
            }
            if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
                self.inviteVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            }else{
                self.modalPresentationStyle=UIModalPresentationCurrentContext;
            }
            [UIView animateWithDuration:0.5 animations:^{
                self.backView.alpha = 1.f;
            } completion:^(BOOL finished) {
                
            }];
            UIViewController *controller = [UIApplication sharedApplication].keyWindow.rootViewController;
            [controller presentViewController:self.inviteVC animated:YES completion:^{
                
                
            }];
        }
            break;
            
        case 7:
        {
            //联系客服
            CLZWebviewViewController *webVC=[[CLZWebviewViewController alloc] init];
            webVC.webTitle=@"常见问题";
            webVC.webUrl=[NSString stringWithFormat:@"%@%@",DOMAIN_ADDRESS,URL_NORMAL_QUESTION];
            [self.navigationController pushViewController:webVC animated:YES];
            
        }
            break;
        default:
            break;
    }
    
}
//-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
//        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
//    }
//
//    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
//        [cell setSeparatorInset:UIEdgeInsetsMake(0,0, 0, 0)];
//    }
//
//}
//-(void)viewDidLayoutSubviews
//{
//    if ([self.displayView respondsToSelector:@selector(setSeparatorInset:)]) {
//        [self.displayView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
//    }
//
//    if ([self.displayView respondsToSelector:@selector(setLayoutMargins:)]) {
//        [self.displayView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
//    }
//}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    //    CGFloat yOffset  = scrollView.contentOffset.y;
    //    if (yOffset < - 180 ) {
    //        CGRect f =  headView.frame;
    //        f.origin.y = yOffset;
    //        f.size.height =  -yOffset;
    //        f.size.width=f.size.height*scale;
    //        headView.frame = f;
    //    }
    
}


#pragma mark Notification
- (void)setupNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(accountDidUpdatedNotification)
                                                 name:ACCOUNT_DID_UPDATED_NOTIFICATION
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shareWXSuccess:) name:@"WXShareSuccess" object:nil];
}

- (void)shareWXSuccess:(NSNotification *)notify {
    self.backView.alpha = 0.f;
    [self.inviteVC dismissViewControllerAnimated:YES completion:^{
        SendMessageToWXResp *response = notify.object;
        if (response.errCode == 0) {
            [[LKDHud shareHud] showtip:@"分享成功"];
        } else {
            [[LKDHud shareHud] showtip:response.errStr];
        }
    }];
}

- (void)cleanupNotification {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)accountDidUpdatedNotification {
    [headView refreshHeadView]; // 刷新header里面的个人信息
    [self.displayView reloadData];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
